package com.ejd.web.vo.manage.goods;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.richfaces.component.UIDataTable;
import com.ejd.service.ISubCategoryService;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.utils.UIComponentUtil;
import com.ejd.utils.ValidatorUtil;
import com.ejd.web.bo.impl.SubCategory;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.GoodsPropertiesVo;
import com.ejd.web.vo.common.GoodsVo;
import com.ejd.web.vo.genl.ExistSubCategoryBean;

public class GoodsCrudBean {

	private GoodsVo goods = new GoodsVo();
	private SelectItem[] secondCategoryItems;//for all
	private String displayMessage;//for has or none goods
	public List<String> errorMessages = new ArrayList<String>();//for validate
	public UIDataTable propertiesDataTable = new UIDataTable();
	//create or update shoes properties

	//end
	public GoodsCrudBean(){
		
	}

	public GoodsVo getGoods() {
		return goods;
	}

	public void setGoods(GoodsVo goods) {
		this.goods = goods;
	}

	public SelectItem[] getSecondCategoryItems() {
		return secondCategoryItems;
	}

	public void setSecondCategoryItems(SelectItem[] secondCategoryItems) {
		this.secondCategoryItems = secondCategoryItems;
	}

	public String getDisplayMessage() {
		return displayMessage;
	}

	public void setDisplayMessage(String displayMessage) {
		this.displayMessage = displayMessage;
	}

	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public UIDataTable getPropertiesDataTable() {
		return propertiesDataTable;
	}

	public void setPropertiesDataTable(UIDataTable propertiesDataTable) {
		this.propertiesDataTable = propertiesDataTable;
	}

	public String deleteGoodsProperties(){//same as create and update
		//String color = (String) SpringFacesUtil.getRequestParameter("paramColor");
		//String size = (String) SpringFacesUtil.getRequestParameter("paramSize");
		Map<String,UIComponent> componentMap = new HashMap<String,UIComponent>();
		List<UIComponent> te= new ArrayList<UIComponent>();
		UIViewRoot currentViewRoot = FacesContext.getCurrentInstance().getViewRoot();
		for(UIComponent component : currentViewRoot.getChildren()){ 
            te= UIComponentUtil.getComponentChildren(component,componentMap); 
        }
		UIDataTable tempTable = (UIDataTable) componentMap.get("table");
		GoodsPropertiesVo deleteData = (GoodsPropertiesVo)tempTable.getRowData();
		this.getGoods().getPropertiesv().remove(deleteData);
		
		return null;
	}
	public Object getIdItem(GoodsPropertiesVo item) {
		return item.getId();
	}
	public String changePrimaryCategoryItems(ValueChangeEvent event) {
		String newValue = event.getNewValue().toString();
		ExistSubCategoryBean existSubCategory = (ExistSubCategoryBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.EXIST_SUB_CATEGORY_BEAN_NAME);
		SelectItem[] allItems = existSubCategory.getSubCategoryItems();
		SelectItem[] currSecondCategoryItems = null;
		this.getGoods().setPrimaryCategoryCode(newValue);
		if (null == this.getGoods().getPrimaryCategoryCode() || "".equals(this.getGoods().getPrimaryCategoryCode()) ) {
			currSecondCategoryItems = allItems;
		}
		else {
			ISubCategoryService<SubCategory,Long> subCategoryService = (ISubCategoryService<SubCategory,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.SUB_CATEGORY_SERVICE_BEAN_NAME);
			subCategoryService.setEntityClass(SubCategory.class);
			subCategoryService.setPKClass(Long.class);
			List<SubCategory> subCategorys = subCategoryService.findBy("categoryCode", this.getGoods().getPrimaryCategoryCode());
			if (null != subCategorys && !(subCategorys.isEmpty())) {
				SelectItem[] tempSubCategoryItems = new SelectItem[subCategorys.size()];
				this.getGoods().setSecondCategoryCode(subCategorys.get(0).getSubCategoryCode());
				for (int i=0; i< tempSubCategoryItems.length; i++) {
					SubCategory subCategory = (SubCategory) subCategorys.get(i);
					String subCategoryNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,subCategory.getSubCategoryName(), FacesMessage.SEVERITY_ERROR).getDetail();
					tempSubCategoryItems[i] = new SelectItem(subCategory.getSubCategoryCode(),subCategoryNameN18,subCategory.getCategoryCode());
				}
				currSecondCategoryItems = tempSubCategoryItems;
			} else {
				this.getGoods().setSecondCategoryCode("");
			}
			
		}
		clearSpecialFields(this.getGoods());
		clearErrorMessages();
		this.setSecondCategoryItems(currSecondCategoryItems);
		return null;
	}
	protected void clearSpecialFields(GoodsVo goodsVo){
		
		if (null != goodsVo) {
			
			if (null != goodsVo.getDfield1()) {
				goodsVo.setDfield1(null);
			}
			if (null != goodsVo.getDfield2()) {
				goodsVo.setDfield2(null);
			}
			if (null != goodsVo.getDfield3()) {
				goodsVo.setDfield3(null);
			}
			if (null != goodsVo.getDfield4()) {
				goodsVo.setDfield4(null);
			}
			if (null != goodsVo.getDfield5()) {
				goodsVo.setDfield5(null);
			}
			if (null != goodsVo.getSfield1() && !("".equals(goodsVo.getSfield1()))) {
				goodsVo.setSfield1("");
			}
			if (null != goodsVo.getSfield2() && !("".equals(goodsVo.getSfield2()))) {
				goodsVo.setSfield2("");
			}
			if (null != goodsVo.getSfield3() && !("".equals(goodsVo.getSfield3()))) {
				goodsVo.setSfield3("");
			}
			if (null != goodsVo.getSfield4() && !("".equals(goodsVo.getSfield4()))) {
				goodsVo.setSfield4("");
			}
			if (null != goodsVo.getSfield5() && !("".equals(goodsVo.getSfield5()))) {
				goodsVo.setSfield5("");
			}
			if (null != goodsVo.getSfield6() && !("".equals(goodsVo.getSfield6()))) {
				goodsVo.setSfield6("");
			}
			if (null != goodsVo.getSfield7() && !("".equals(goodsVo.getSfield7()))) {
				goodsVo.setSfield7("");
			}
			if (null != goodsVo.getSfield8() && !("".equals(goodsVo.getSfield8()))) {
				goodsVo.setSfield8("");
			}
			if (null != goodsVo.getSfield9() && !("".equals(goodsVo.getSfield9()))) {
				goodsVo.setSfield9("");
			}
			if (null != goodsVo.getSfield10() && !("".equals(goodsVo.getSfield10()))) {
				goodsVo.setSfield10("");
			}
			if (null != goodsVo.getPropertiesv()) {
				goodsVo.getPropertiesv().clear();
			}
		}
		
	}
	protected void clearErrorMessages() {
		if (null != this.getErrorMessages() && !(this.getErrorMessages().isEmpty())) {
			this.getErrorMessages().clear();
		}
	}
	protected void validateGoods(GoodsVo goodsVo, List<String> errorMessages) {
		if (null == errorMessages) {
			errorMessages = new ArrayList<String>();
		}
		errorMessages.clear();
		if (EjdConstants.PRIMARY_CATEGORY_CODE_SHOES.equals(goodsVo.getPrimaryCategoryCode())) {
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.apply.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield1(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.material.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield2(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "shoes.lining.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield3(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "shoes.class.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield4(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.style.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield5(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "shoes.basemetal.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield6(), message.getDetail());
			if (null == goodsVo.getPropertiesv() || goodsVo.getPropertiesv().isEmpty()) {
				message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.colorsizenumber.must.input.error.message", FacesMessage.SEVERITY_ERROR);
				errorMessages.add(message.getDetail());
			}
		}
		if (EjdConstants.PRIMARY_CATEGORY_CODE_BAGS.equals(goodsVo.getPrimaryCategoryCode())) {
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.apply.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield1(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.material.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield2(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.color.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield3(), message.getDetail());
		}
		if (EjdConstants.PRIMARY_CATEGORY_CODE_CLOTHING.equals(goodsVo.getPrimaryCategoryCode())) {
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.apply.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield1(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.material.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield2(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "clothing.pattern.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield3(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "clothing.collar.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield4(), message.getDetail());
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.style.display", FacesMessage.SEVERITY_ERROR);
			ValidatorUtil.EmptyValidatorForSelect(errorMessages, goodsVo.getSfield5(), message.getDetail());
			if (null == goodsVo.getPropertiesv() || goodsVo.getPropertiesv().isEmpty()) {
				message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.colorsizenumber.must.input.error.message", FacesMessage.SEVERITY_ERROR);
				errorMessages.add(message.getDetail());
			}
		}
		
	}
	public String codeKeyUp() {
		this.getGoods().setImageName(this.getGoods().getCode() + EjdConstants.IMAGE_NAME_SUFFIX);
		this.getGoods().setIntroduceFileName(this.getGoods().getCode() + EjdConstants.INTRODUCE_FILE_NAME_SUFFIX);
		return null;
	}
}
